﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using THSLib;
using THSERP.BusinessEntities;
using THSERP.BaseProvider;
using System.Transactions;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;


namespace THSERP.Modules.Food
{
    public partial class guiAddItems : THSERPScreen
    {
        int startIndex;
        TreeList treeList = null;
        //THSList<ICProductsInfo> lstItem = new THSList<ICProductsInfo>();
        THSList<ICProductToFoodGroupsInfo> listItem = new THSList<ICProductToFoodGroupsInfo>();
        GridHitInfo downHitInfo = null;

        public guiAddItems()
        {
            InitializeComponent();
        }
        public guiAddItems(TreeList _treeList)
        {
            InitializeComponent();
            this.treeList = _treeList;
            //lstItem = new THSList<ICProductsInfo>();
            listItem = new THSList<ICProductToFoodGroupsInfo>();
            InitializeModuleNonSection();
            InitializeModuleSection();
            DataSet ds = new ICProductsController().GetAllObjects();
            lkeProducts.DataSource = ds.Tables[0];
            
        }

        private void fld_btnCloseModule_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void InitializeModuleSection()
        {
            TreeListNode node = treeList.FocusedNode;
            if (node != null)
            {
                LIFoodGroupsInfo objLIFoodGroupsInfo = (LIFoodGroupsInfo)new LIFoodGroupsController().GetObjectByName(node.GetDisplayText(treeList.Columns[0]));
                if (objLIFoodGroupsInfo != null)
                {
                    DataSet ds = new ICProductToFoodGroupsController().GetAllDataByForeignColumn("LIFoodGroupID", objLIFoodGroupsInfo.LIFoodGroupID);
                    if (ds != null)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            ICProductToFoodGroupsInfo objICProductToFoodGroupsInfo = (ICProductToFoodGroupsInfo)new ICProductToFoodGroupsController().GetObjectFromDataRow(row);
                            if (objICProductToFoodGroupsInfo != null)
                            {
                                listItem.Add(objICProductToFoodGroupsInfo);
                                //ICProductsInfo objICProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(objICProductToFoodGroupsInfo.ICProductID);
                                //if (objICProductsInfo != null)
                                //{
                                //    lstItem.Add(objICProductsInfo);
                                //}
                            }
                        }
                    }
                }
            }
            fld_dgcItemFoodGroup.DataSource = listItem;
            fld_dgvItemFoodGroup.RefreshData();

        }
        public void InitializeModuleNonSection()
        {
            DataSet ds = new ICProductsController().GetAllObjects();
            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow row = dt.Rows[i];
                    if (!row["ICProductTypeCombo"].ToString().Equals("Item"))
                    {
                        dt.Rows.Remove(row);
                        i--;
                    }
                }
            }
            fld_dgcItemNonFoodGroup.DataSource = dt;
            fld_dgvItemNonFoodGroup.RefreshData();
        }

        private void fld_btnForward_Click(object sender, EventArgs e)
        {
            int iRowHandle = fld_dgvItemNonFoodGroup.FocusedRowHandle;
            if (iRowHandle >= 0)
            {
               DataRow row = fld_dgvItemNonFoodGroup.GetDataRow(iRowHandle);
               ICProductsInfo objICProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectFromDataRow(row);
               if (objICProductsInfo != null)
               {
                   if (listItem.Exists("ICProductID", objICProductsInfo.ICProductID))
                   {
                       MessageBox.Show("Món này đã tồn tại trong ngày ngày.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       return;
                   }
                   TreeListNode node = treeList.FocusedNode;
                   if (node != null)
                   {
                       LIFoodGroupsInfo objLIFoodGroupsInfo = (LIFoodGroupsInfo)new LIFoodGroupsController().GetObjectByName(node.GetDisplayText(treeList.Columns[0]));
                       if (objLIFoodGroupsInfo != null)
                       {
                           ICProductToFoodGroupsInfo objICProductToFoodGroupsInfo = new ICProductToFoodGroupsInfo();
                           objICProductToFoodGroupsInfo.ICProductID = objICProductsInfo.ICProductID;
                           objICProductToFoodGroupsInfo.LIFoodGroupID = objLIFoodGroupsInfo.LIFoodGroupID;
                           objICProductToFoodGroupsInfo.ICProductToFoodGroupSortOder = listItem.Count+1;
                            new ICProductToFoodGroupsController().CreateObject(objICProductToFoodGroupsInfo);
                           listItem.Add(objICProductToFoodGroupsInfo);
                       }
                   }
               }
            }
            fld_dgcItemFoodGroup.DataSource = listItem;
            fld_dgvItemFoodGroup.RefreshData();
        }

        //private void fld_btnSaveMUGSection_Click(object sender, EventArgs e)
        //{
        //    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
        //    {
        //        try
        //        {
        //            TreeListNode node = treeList.FocusedNode;
        //            if (node != null)
        //            {
        //                LIFoodGroupsInfo objLIFoodGroupsInfo = (LIFoodGroupsInfo)new LIFoodGroupsController().GetObjectByName(node.GetDisplayText(treeList.Columns[0]));
        //                if (objLIFoodGroupsInfo != null)
        //                {
        //                    //delete all 
        //                    new ICProductToFoodGroupsController().DeleteAllICProductToFoodGroupByFoodGroupID(objLIFoodGroupsInfo.LIFoodGroupID);
        //                    int iSortOrder = 0;
        //                    foreach (ICProductsInfo objICProductsInfo in listItem)
        //                    {
        //                        iSortOrder++;
        //                        ICProductToFoodGroupsInfo objICProductToFoodGroupsInfo = new ICProductToFoodGroupsInfo();
        //                        objICProductToFoodGroupsInfo.ICProductID = objICProductsInfo.ICProductID;
        //                        objICProductToFoodGroupsInfo.LIFoodGroupID = objLIFoodGroupsInfo.LIFoodGroupID;
        //                        objICProductToFoodGroupsInfo.ICProductToFoodGroupSortOder = iSortOrder;
        //                        new ICProductToFoodGroupsController().CreateObject(objICProductToFoodGroupsInfo);

        //                    }
        //                }
        //            }
        //            scope.Complete();
        //        }
        //        catch (Exception ex)
        //        {
        //            scope.Dispose();
        //        }
        //    }
        //    ((FoodModule)this.Module).InitializeTreeList(treeList);
        //    this.Close();
        //}
        private void fld_btnSaveMUGSection_Click(object sender, EventArgs e)
        {
            ((FoodModule)this.Module).InitializeTreeList(treeList);
            this.Close();
            
        }


        private void fld_btnBackward_Click(object sender, EventArgs e)
        {
            int iRowHandle = fld_dgvItemFoodGroup.FocusedRowHandle;
            if (iRowHandle >= 0)
            {
                ICProductToFoodGroupsInfo objICProductToFoodGroupsInfo = (ICProductToFoodGroupsInfo)fld_dgvItemFoodGroup.GetRow(iRowHandle);
                if (objICProductToFoodGroupsInfo != null)
                {
                    new ICProductToFoodGroupsController().DeleteObject(objICProductToFoodGroupsInfo.ICProductToFoodGroupID);
                    int iIndex = listItem.IndexOf(objICProductToFoodGroupsInfo);
                    listItem.RemoveAt(iIndex);
                }
            }
            fld_dgcItemFoodGroup.DataSource = listItem;
            fld_dgvItemFoodGroup.RefreshData();
        }

        /// <summary>
        /// Do drag/drop gridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks>Hieu.nguyen_04/05/2010</remarks>
        #region Do drag/drop
        private void fld_dgvModuleSection_MouseDown(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            downHitInfo = null;

            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
            if (Control.ModifierKeys != Keys.None) return;
            if (e.Button == MouseButtons.Left && hitInfo.InRow)
                downHitInfo = hitInfo;

        }

        private void fld_dgcModuleSection_DragDrop(object sender, DragEventArgs e)
        {
            //GridControl grid = sender as GridControl;
            //GridView view = grid.MainView as GridView;
            //GridHitInfo srcHitInfo = e.Data.GetData(typeof(GridHitInfo)) as GridHitInfo;
            //GridHitInfo hitInfo = view.CalcHitInfo(grid.PointToClient(new Point(e.X, e.Y)));
            //int sourceRow = srcHitInfo.RowHandle;
            //int targetRow = hitInfo.RowHandle;
            //MoveRow(sourceRow, targetRow);

        }

        private void fld_dgcModuleSection_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.None;

            GridHitInfo downHitInfo = e.Data.GetData(typeof(GridHitInfo)) as GridHitInfo;
            if (downHitInfo != null)
            {
                GridControl grid = sender as GridControl;
                GridView view = grid.MainView as GridView;
                GridHitInfo hitInfo = view.CalcHitInfo(grid.PointToClient(new Point(e.X, e.Y)));
                if (hitInfo.InRow && hitInfo.RowHandle != downHitInfo.RowHandle)
                    e.Effect = DragDropEffects.Move;
            }

        }

        private void fld_dgvModuleSection_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);

                if (!dragRect.Contains(new Point(e.X, e.Y)))
                {
                    view.GridControl.DoDragDrop(downHitInfo, DragDropEffects.All);
                    downHitInfo = null;
                }
            }   
        }

        private void MoveRow(int sourceRow, int targetRow)
        {
            //if (sourceRow == targetRow) return;
            //ICProductsInfo temp = (ICProductsInfo)lstItem[sourceRow].Clone();
            //lstItem.RemoveAt(sourceRow);
            //lstItem.Insert(targetRow, temp);
            //fld_dgcItemFoodGroup.DataSource = lstItem;
            //fld_dgvItemFoodGroup.RefreshData();
        }
#endregion

        private void fld_dgvItemNonFoodGroup_DoubleClick(object sender, EventArgs e)
        {

            int iRowHandle = fld_dgvItemNonFoodGroup.FocusedRowHandle;
            if (iRowHandle >= 0)
            {
                DataRow row = fld_dgvItemNonFoodGroup.GetDataRow(iRowHandle);
                ICProductsInfo objICProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectFromDataRow(row);
                if (objICProductsInfo != null)
                {
                    if (listItem.Exists("ICProductID", objICProductsInfo.ICProductID))
                    {
                        MessageBox.Show("Món này đã tồn tại trong ngày ngày.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    TreeListNode node = treeList.FocusedNode;
                    if (node != null)
                    {
                        LIFoodGroupsInfo objLIFoodGroupsInfo = (LIFoodGroupsInfo)new LIFoodGroupsController().GetObjectByName(node.GetDisplayText(treeList.Columns[0]));
                        if (objLIFoodGroupsInfo != null)
                        {
                            ICProductToFoodGroupsInfo objICProductToFoodGroupsInfo = new ICProductToFoodGroupsInfo();
                            objICProductToFoodGroupsInfo.ICProductID = objICProductsInfo.ICProductID;
                            objICProductToFoodGroupsInfo.LIFoodGroupID = objLIFoodGroupsInfo.LIFoodGroupID;
                            objICProductToFoodGroupsInfo.ICProductToFoodGroupSortOder = listItem.Count + 1;
                            new ICProductToFoodGroupsController().CreateObject(objICProductToFoodGroupsInfo);
                            listItem.Add(objICProductToFoodGroupsInfo);
                        }
                    }
                }
            }
            fld_dgcItemFoodGroup.DataSource = listItem;
            fld_dgvItemFoodGroup.RefreshData();
        }

        private void fld_dgvItemFoodGroup_DoubleClick(object sender, EventArgs e)
        {
            int iRowHandle = fld_dgvItemFoodGroup.FocusedRowHandle;
            if (iRowHandle >= 0)
            {
                ICProductToFoodGroupsInfo objICProductToFoodGroupsInfo = (ICProductToFoodGroupsInfo)fld_dgvItemFoodGroup.GetRow(iRowHandle);
                if (objICProductToFoodGroupsInfo != null)
                {
                    new ICProductToFoodGroupsController().DeleteObject(objICProductToFoodGroupsInfo.ICProductToFoodGroupID);
                    int iIndex = listItem.IndexOf(objICProductToFoodGroupsInfo);
                    listItem.RemoveAt(iIndex);
                }
            }
            fld_dgcItemFoodGroup.DataSource = listItem;
            fld_dgvItemFoodGroup.RefreshData();
        }

    }
}